<?

$name = strtolower($_POST['name']);
$pass1 = $_POST['pass1'];
$pass2 = $_POST['pass2'];

if( $pass1 == $pass2 ) {
	exec("/usr/sbin/smbldap-useradd -am $name 2> /dev/null", $out, $ret);
	switch($ret) {
		case 9:
			error('user_exist');
			break;
		case 0:
			$pw = $pass1;

                        //byter lösen med smbldap-usermod
                        $descriptorspec = array(
                           0 => array("pipe", "r"),  // stdin
                           1 => array("pipe", "w"),  // stdout
                           2 => array("pipe", "w")   // stderr
                        );
                        $process = proc_open('exec /usr/sbin/smbldap-usermod -PUJ '.escapeshellarg($name), $descriptorspec, $pipes, "/");
                        fwrite($pipes[0], $pw . "\n");
                        fwrite($pipes[0], $pw . "\n");
                        $stout_str = stream_get_contents($pipes[1]);
                        $sterr_str = stream_get_contents($pipes[2]);
                        fclose($pipes[0]);
                        fclose($pipes[1]);
                        fclose($pipes[2]);
                        $exitCode = proc_close($process);
                        if( $exitCode !== 0 && strlen($sterr_str) !== 0 ) {
                                //echo "FAIL: $stout_str\n$sterr_str\n";
                                error('unknown');
                        }
		
			//mailalias
			$addarr["cn"] = $name;
			$addarr["objectClass"] = "nisMailAlias";
			$addarr["rfc822MailMember"] = "$name";
			$dn = "cn=$name,ou=Aliases,".$GLOBALS["dc"];
			ldap_add($conn,$dn,$addarr);
	
			//tar bort förvirrande "System User" som Fullständigt namn
			$change["displayName"] = $name;
			$change["gecos"] = $name;
			$dn = "uid=$name,ou=Users,".$GLOBALS["dc"];
			ldap_mod_replace($conn,$dn,$change);
			break;
		default:
			error('unknown');
	}
}
else {
	error('password_diff');
}

?>
